package com.ciwong.epaper.modules.epaper.ui.newlistenspeak.longcoversation;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.Questions;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.widget.RoundProgressBar;
import com.ciwong.libs.utils.CWSys;

/**
 * Created by Administrator on 2017/2/14.
 */
public class LongConversationBottomView extends LinearLayout {

    private final String TAG = "bottomview";
    public static final int SKIP_TYPE_NULL = -1;
    public static final int SKIP_TYPE_COUNT_DOWN = 2;
    public static final int SKIP_TYPE_PLAY_STEM = 3;
    //选择填空类型
    public static final int SKIP_TYPE_CHOICE = 4;

    protected ImageView problemPlay;
    private TextView tvTimer;
    private TextView tx_skip;
    protected View timerLayout;//准备朗读  倒计时  底部文字栏 进度条上文字
    private RoundProgressBar round_progress_bar;
    private ImageView img_read_ready;
    private ImageView img_reply;
    private TextView tx_lsw_next_tips;


    private View handle_state_bar;

    public LongConversationBottomView(Context context) {
        this(context, null);
    }

    public LongConversationBottomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LongConversationBottomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(this.getContext()).inflate(R.layout.listen_speak_bottom_view, this);
        initView();
    }

    private void initView() {

        round_progress_bar = (RoundProgressBar) findViewById(R.id.round_progress_bar);
        round_progress_bar.setCricleProgressColor(getResources().getColor(R.color.color_light_green));
        problemPlay = (ImageView) findViewById(R.id.problemPlay);
        timerLayout = findViewById(R.id.ls_timer_layout);// 时间提示layout
        tvTimer = (TextView) findViewById(R.id.tv_timer);// 时间提示
        tx_skip = (TextView) findViewById(R.id.tx_skip);

        img_read_ready = (ImageView) findViewById(R.id.img_read_ready);
        img_reply = (ImageView) findViewById(R.id.img_reply);
        tx_lsw_next_tips = (TextView) findViewById(R.id.tx_lsw_next_tips);
        handle_state_bar = findViewById(R.id.handle_state_bar);
    }


    /**
     * 设置播放按钮状态
     *
     * @param isShow
     */
    public void setPlayBtnState(boolean isShow) {
        if (problemPlay == null) {
            return;
        }
        if (isShow) {
            problemPlay.setVisibility(View.VISIBLE);
            Drawable drawable = problemPlay.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }

        } else {
            problemPlay.setVisibility(View.GONE);
            Drawable drawable = problemPlay.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    public void setAnswerState(boolean isAnswerState) {
        if (isAnswerState) {
            img_reply.setVisibility(VISIBLE);
        } else {
            img_reply.setVisibility(GONE);
        }

    }


    /**
     * 显示播放按钮
     */
    public void showPlayButton(int mQuestionType) {
        if (mQuestionType == Questions.QuestionType.QUESTION_TYPE_CHOICE
                ) {
            setPlayBtnState(false);
        } else {
            setPlayBtnState(true);
            microRecordingSetVisibility(View.GONE);
        }
    }

    public void microRecordingSetVisibility(int visibility) {

    }


    /**
     * 更新进度显示
     *
     * @param sequence
     */
    public void updateTimer(CharSequence sequence, int progress, boolean showSkipBtn, int skipType, ListenSpeakUtil.State status, boolean isLastQuestion) {

        if (showSkipBtn) {
            if (skipType == SKIP_TYPE_CHOICE) {
                if (!isLastQuestion) {
                    tx_skip.setText(getContext().getString(R.string.str_next_question));
                } else {
                    tx_skip.setText(getContext().getString(R.string.str_complete_answer));
                }

                tx_skip.setVisibility(VISIBLE);

            } else {

                tx_skip.setText(getContext().getString(R.string.str_skip));
                tx_skip.setVisibility(VISIBLE);

            }

        } else {
            tx_skip.setVisibility(GONE);
        }

        showTimerAnimation(skipType);

        tvTimer.setText(sequence);
        boolean isShow = false;
        switch (status) {
            case LOOK_CHILD_QUESTION:// 子题阅题
                img_read_ready.setImageResource(R.mipmap.icon_read_ready);
                img_read_ready.setVisibility(VISIBLE);
                isShow = CWSys.getSharedBoolean(EConstants.SHARE_KEY_LSW_TIPS_READ + EApplication.getInstance().getUserInfoBase().getUserId(), false);
                if (!isShow) {
                    tx_lsw_next_tips.setVisibility(VISIBLE);
                    tx_lsw_next_tips.setText(getResources().getString(R.string.str_lsw_next_tips_read));
                } else {
                    tx_lsw_next_tips.setVisibility(INVISIBLE);
                }
                setAnswerState(false);
                setPlayBtnState(false);

                break;
            case BIG_QUESTION_STEM:// 播放大题题干音频
                //展示提示对话框
                isShow = CWSys.getSharedBoolean(EConstants.SHARE_KEY_LSW_TIPS_PLAY + EApplication.getInstance().getUserInfoBase().getUserId(), false);
                if (!isShow) {
                    tx_lsw_next_tips.setVisibility(VISIBLE);
                    tx_lsw_next_tips.setText(getResources().getString(R.string.str_lsw_next_tips_play));
                } else {
                    tx_lsw_next_tips.setVisibility(INVISIBLE);
                }

            case SMALL_QUESTION_STEM:// 播放小题音频
            case CHILD_QUESTION_STEM: // 播放子题音频
                img_read_ready.setVisibility(GONE);
                setAnswerState(false);
                setPlayBtnState(true);

                break;
            case READY_SMALL_QUESTION:// 小题准备
            case READY_CHILD_QUESTION:// 子题准备
                img_read_ready.setImageResource(R.mipmap.icon_counting_ready);
                img_read_ready.setVisibility(VISIBLE);
                isShow = CWSys.getSharedBoolean(EConstants.SHARE_KEY_LSW_TIPS_PREPARE + EApplication.getInstance().getUserInfoBase().getUserId(), false);
                if (!isShow) {

                    Log.d(TAG, "############点击提前结束准备时间############");

                    tx_lsw_next_tips.setVisibility(VISIBLE);
                    tx_lsw_next_tips.setText(getResources().getString(R.string.str_lsw_next_tips_prepare));
                } else {
                    tx_lsw_next_tips.setVisibility(INVISIBLE);
                }

                setAnswerState(false);
                setPlayBtnState(false);
                break;

            case ANSWER:// 答题
                img_read_ready.setVisibility(GONE);
                //                microRecording.setVisibility(VISIBLE);
                if (skipType == SKIP_TYPE_CHOICE) {

                    isShow = CWSys.getSharedBoolean(EConstants.SHARE_KEY_LSW_TIPS_ANSWER + EApplication.getInstance().getUserInfoBase().getUserId(), false);
                    if (!isShow) {
                        tx_lsw_next_tips.setVisibility(VISIBLE);
                        tx_lsw_next_tips.setText(getResources().getString(R.string.str_lsw_next_tips_answer));
                    } else {
                        tx_lsw_next_tips.setVisibility(INVISIBLE);
                    }
                }
                setAnswerState(true);
                setPlayBtnState(false);
                break;

            case END_AUDIO: // 录音结束
                img_read_ready.setVisibility(GONE);
                setAnswerState(false);
                setPlayBtnState(false);
                break;
            default:
                break;
        }
        round_progress_bar.setProgress(progress);
    }


    public void setNextTipsVisibility(int visibility) {
        tx_lsw_next_tips.setVisibility(visibility);
    }


    /**
     * 隐藏时间计时TextView
     */
    public void hideTimerAnimation(ListenSpeakUtil.State status) {
        if (timerLayout.isShown()) {
            timerLayout.setVisibility(View.INVISIBLE);
            round_progress_bar.setVisibility(View.INVISIBLE);
            img_read_ready.setVisibility(INVISIBLE);
            tx_skip.setVisibility(GONE);
        }
    }

    /**
     * 显示时间计时TextView
     */
    private void showTimerAnimation(int skipType) {
        timerLayout.setVisibility(View.VISIBLE);
        round_progress_bar.setVisibility(View.VISIBLE);
    }

    public RoundProgressBar getSeekBarView() {
        return round_progress_bar;
    }


    public void seekBarSetMax(int max) {

        try {
            round_progress_bar.setMax(max);
        } catch (IllegalArgumentException e) {

        }
    }

    public void setStateBarVisibility(int visibility) {
        handle_state_bar.setVisibility(visibility);
    }

    public void setOnSkipListener(OnClickListener listener) {
        if (null != listener) {
            tx_skip.setOnClickListener(listener);
        }

    }


}
